<?php
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: $this <498944516@qq.com>
// +----------------------------------------------------------------------
namespace app\we\model;

use think\Model;

class WeApp extends Model
{

   
    /**
     * 登录/
     */
    public function index($data=''){

      $this->data=$data;
      //验证权限
     
    	   
       
       switch ($this->data['keyword']) {
          
          case 'F':
            $reply= $this->F();
            break;
          case 'Q':
            $reply= $this->Q();
            break;
       
          default:
            # code...
            break;
        } 



       return $reply;
      
    }


    
   
    public function F() {
       if (!$this->data['before'] and !$this->data['back']) {
         $message='F -粉丝(fans)管理工具';
         $message.="\n-------------------";
         $message.="\n/:hug0.登录：\n17092610050FA123 (账号FA密码)";
         $message.="\n/:hug1.查询：\n1F (查询1号粉丝信息)";
         $message.="\n/:hug2.充值：\n1FM1 (给1号粉丝充值1元)";
         $message.="\n/:hug3.积分：\n1FS1 (给1号粉丝集1分)";
         $message.="\n/:hug4.消息：\n1FC文字 (给1号粉丝发送一句话)";
         $message.="\n/:hug5.卡券：\n1FC卡 (获取所有可用卡券)";
         $message.="\n/:hug6.卡券：\n1FC卡2 (给1号粉丝发2号卡券)";
         $message.="\n/:hug7.消费：\n1F2+100 (1号粉丝正2号桌消费100元)";
         $message.="\n/:hug8.消费：\n1F2++100 (同上,强制积分)";
        
         return $message;
       }


       //查询自己的账户{粉丝OPENID}F
       if ($this->data['before'] and !$this->data['back']) {
        if (is_numeric($this->data['before'])){
            $admin=model('we/WeFans')->checkAdmin($this->data['openid']);
            if($admin['status']==0) return $admin['text']; 
        }
        return model('we/WeFans')->infoForFansByOrder($this->data);
       }


       if ($this->data['before'] and $this->data['back']) return model('we/WeFans')->F($this->data);
    }

    public function Q(){
    //获取用户头像，二维码合并成推广图片
    //发送图片
    //用户识别图片
    //响应机器人完成业务
    //TODO增加永久二维码
  
    switch ($this->data['before']) {

      case '临时二维码':
     
        get_file_from_net($this->data['fans']['headimgurl'],"uploads/we/avatar",$this->data['fans']['openid'].'.jpg');
       
        $this_avatar_pic="uploads/we/avatar/".$this->data['fans']['openid'].'.jpg';
       
        $image = \think\Image::open($this_avatar_pic);
        $image->thumb(250,250,\think\Image::THUMB_CENTER)->save($this_avatar_pic);
 
        $qrcode=get_qrcode($this->data['appid']);

        get_file_from_net($qrcode['short_url'],"uploads/we/qrocde0",$qrcode['appid'].'_'.$qrcode['scene_id'].'.jpg');
        $this_qrcode_pic="uploads/we/qrocde0/".$qrcode['appid'].'_'.$qrcode['scene_id'].'.jpg';

        $qrcode['loc']=$this_qrcode_pic;
        $qrcode['aid']=$this->data['aid'];
        $qrcode['title']=$this->data['fans']['nickname'];
        $qrcode['code']=$this->data['fans']['openid'];
        $qrcode['subscribe_answer']='关注Q';
        $qrcode['SCAN_answer']='扫描Q';
        $res = model('we/WeSceneQrcode')->allowField(true)->data($qrcode)->save();

        
        $image = \think\Image::open($this_qrcode_pic);
        $image->thumb(170,170,\think\Image::THUMB_CENTER)->save($this_qrcode_pic);
       
        $image = \think\Image::open(get_cover($this->data['options']['tui_pic'], 'path'));
        $image->water($this_avatar_pic,[150,200]);
        $image->water($this_qrcode_pic,7);
        $image->text('有效期至'.date('Y-m-d H:i',time()+604800),  'static/admin/fonts/HYQingKongTiJ.ttf', 24,'#ffffff',7,[190,-20]);
        $image->text("长按图片识别二维码领福利\n随机发放饮品券，菜品券\n10-300元卡券，霸王餐券",  'static/admin/fonts/HYQingKongTiJ.ttf',30,'#551A8B',7,[170,-550]);
        $this_ok_pic="uploads/we/qrocde0/tui_".$this->data['fans']['openid'].'.jpg';
        $image->save($this_ok_pic);


        $pic=upload_media($this->data['appid'],$this_ok_pic);
        
        $reply['touser']=$this->data['fans']['openid'];
        $reply['msgtype']='image';
        $reply['image']=['media_id'=>$pic['media_id']];

        custom_message($this->data['openid'],$reply);
        break;

      case '关注':
        //查询这个顾客是谁推荐的
      
        $qrcode=model('we/WeSceneQrcode')->infoById($this->data['qrcode_id']);
     
        if ($qrcode['code']) $fans=model('we/WeFans')->info($qrcode['code']);
        if($this->data['subscribe_openid']){
          $res=db('WeFans')->where('openid',$this->data['openid'])->update(['subscribe'=>1,'subscribe_openid'=>$fans['openid'],'subscribe_time'=>time()]);
          $message=$fans['nickname'].'你推广的粉丝'.$this->data["nickname"].'关注了我们,消费后你将得到佣金！';
        //这里应该是个模板消息
     
        custom_message($fans['openid'],['msgtype'=>'text','text'=>['content'=>$message]]);
        }
       
       
        // $reply=model('we/WeFans')->cfoAmount($fans,0.1,$this->data,$message);
        return '终于等到你，你扫码了好友'.$fans['nickname'].'的二维码关注我们';


        break;

      case '扫描':
        //查询这个顾客是谁推荐的
      mylog($this->data['qrcode_id'].'99');
        
        $qrcode=model('we/WeSceneQrcode')->infoById($this->data['qrcode_id']);
      
        if ($qrcode['code']) $fans=model('we/WeFans')->info($qrcode['code']);

        custom_message($fans['openid'],['msgtype'=>'text','text'=>['content'=>$this->data['nickname'].'扫了你分享的二维码']]);
        return '你扫码了'.$fans['nickname'].'的微信二维码';


        break;
      
      default:
        # code...
        break;
    }
      

    }

    

    
}